package com.zzt.java_doc_search.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzt.java_doc_search.searcher.DocSearcher;
import com.zzt.java_doc_search.searcher.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-05-13
 * Time: 11:44
 */
@RestController
public class DocSearcherController {

    private static DocSearcher searcher = new DocSearcher();

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 搜索
     * @param query 查询词 来自于请求 URL 的 query string 中的 query 这个 key 的值
     * @return 响应内容
     */
//    @RequestMapping("/searcher")
//    public List<Result> search(@RequestParam("query") String query) throws JsonProcessingException {
//         List<Result> results = searcher.search(query);
//         return results;
////        return objectMapper.writeValueAsString(results);
//    }
    @RequestMapping(value = "/searcher", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String search(@RequestParam("query") String query) throws JsonProcessingException {
        List<Result> results = searcher.search(query);
        return objectMapper.writeValueAsString(results);
    }
}
